package com.linkage.jz.bankruptcy.activity.Project.ZQUser;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.CreateAddrActivity;
import com.linkage.jz.bankruptcy.activity.Project.ZQUser.DetailOper.ChangerLegalUserNameActivity;
import com.linkage.jz.bankruptcy.adapter.FullyGridLayoutManager;
import com.linkage.jz.bankruptcy.bean.MyAddr;
import com.linkage.jz.bankruptcy.bean.ZQUserDetailBaseInfo;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.widget.CommonPromptDialog;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.base.BaseWhiteCreateActivity;
import com.linkage.seapeak.pojo.UploadImage;
import com.linkage.seapeak.utils.MenuBottomSheetDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.http.Field;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangeToMeActivity extends BaseWhiteCreateActivity {


    private static ZQUserDetailBaseInfo mInfo;
    private String creditorId;

    private int REQUEST_CODE_ADDRESS = 10001;
    PublicNoticeDialog noticeDialog;
    private MyAddr mAddrInfo;
    private TextView creditorName;
    private EditText tv_zq_phone;
    private EditText tv_zq_other_phone;
    private EditText tv_zq_id;
    private TextView sendAddress;
    private EditText edit_detail, tv_zq_email;

    private static String mType; //0管理人，1债权人
    private static int mUserType;//0管理人 1 债权人

    protected ShapeLoadingDialog mShapeLoadingDialog;

    private CommonPromptDialog mPromptDialog;
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        mPromptDialog = new CommonPromptDialog(this,CommonPromptDialog.TYPE_TIPS);
        mPromptDialog.setContent("放弃编辑？");
        mPromptDialog.setPositiveClickListener("是", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChangeToMeActivity.super.onBackPressed();
            }
        });
        mPromptDialog.setNegativeClickListener("否", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPromptDialog.dismiss();
            }
        });
        mPromptDialog.show();
    }

    public static void start(Context context, ZQUserDetailBaseInfo info, String creditorId, String type, int userType) {
        Intent intent = new Intent(context, ChangeToMeActivity.class);
//        intent.putExtra("mInfo", mInfo);
        intent.putExtra("creditorId", creditorId);
        mInfo= info;
        mType = type;
        mUserType = userType;
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_tome, "变更为本人操作");
        mShapeLoadingDialog = new ShapeLoadingDialog(this);
        initView();
        addMenu(new MenuAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        }, "提交", 0));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_ADDRESS) {
                sendAddress.setTag(data.getStringExtra("id"));
                sendAddress.setText(data.getStringExtra("desc"));
                mAddrInfo = (MyAddr) data.getSerializableExtra("bean");
                if(mAddrInfo!=null)
                    mInfo.setAddressInfo( new MyAddr().toArrInfo(mAddrInfo));
            }
        }
    }

    @Override
    public void finish() {
//        noticeDialog = new PublicNoticeDialog(ChangeToMeActivity.this, new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (v.getId() == R.id.btn_save_notice) {
                    ChangeToMeActivity.super.finish();
//                }
//                noticeDialog.dismiss();
//            }
//        }, 1, "提示", "放弃编辑？",
//                "否", "是");
//        noticeDialog.show();
    }
    protected View titlebar_back;
    private void initView() {


//        mInfo = (ZQUserDetailBaseInfo) getIntent().getSerializableExtra("mInfo");
        creditorId = getIntent().getStringExtra("creditorId");


        creditorName = (TextView) findViewById(R.id.creditor_name);
        tv_zq_phone = (EditText) findViewById(R.id.tv_zq_phone);
        tv_zq_other_phone = (EditText) findViewById(R.id.tv_zq_other_phone);
        tv_zq_id = (EditText) findViewById(R.id.tv_zq_id);

        sendAddress = (TextView) findViewById(R.id.send_address);
        edit_detail = (EditText) findViewById(R.id.edit_detail);
        tv_zq_email = (EditText) findViewById(R.id.tv_zq_email);

        creditorName.setText(mInfo.getCreditorName());

        sendAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChangeToMeActivity.this, CreateAddrActivity.class);
                intent.putExtra(CreateAddrActivity.KEY_FROM, CreateAddrActivity.FROM_FLOW);
                MyAddr addr = new MyAddr();
                addr =  new MyAddr().toMyArr(mInfo.getAddressInfo());
                intent.putExtra(CreateAddrActivity.KEY_BEAN, addr);
                startActivityForResult(intent, REQUEST_CODE_ADDRESS);


            }
        });

        if(mInfo.getAddressInfo()!=null) {
            sendAddress.setText(mInfo.getAddressInfo().getDetailString());
            sendAddress.setTag(mInfo.getAddressInfo().getId());
            mAddrInfo= new MyAddr().toMyArr(mInfo.getAddressInfo());

        }

//        titlebar_back = findViewById(R.id.top_back1);
//        titlebar_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                onBackPressed();
//            }
//        });
    }

    /**
     * 保存信息
     */
    private void saveData() {


        String creditorName_str = creditorName.getText().toString().trim();
        String tv_zq_phone_str = tv_zq_phone.getText().toString().trim();
        String edit_detail_str = edit_detail.getText().toString().trim();
        String tv_zq_id_str = tv_zq_id.getText().toString().trim();
        if (TextUtils.isEmpty(creditorName_str)) {
//            ToastUtil.showShort("债权人名称不能为空");
            showToast("债权人名称不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
            return;
        }
        if (TextUtils.isEmpty(tv_zq_phone_str)) {
//            ToastUtil.showShort("债权人电话不能为空");
            showToast("债权人电话不能为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
            return;
        }
        if (TextUtils.isEmpty(tv_zq_id_str)) {
//            ToastUtil.showShort("债权人身份证为空");
            showToast("债权人身份证为空", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
            return;
        }
//        if (TextUtils.isEmpty(edit_detail_str)) {
//            ToastUtil.showShort("变更说明不能为空");
//            return;
//        }

        String tv_zq_other_phone_str = tv_zq_other_phone.getText().toString().trim();
        String tv_zq_email_str = tv_zq_email.getText().toString().trim();

        String sendAddressid = (String) sendAddress.getTag();
        if (sendAddressid == null) {
            sendAddressid = "";
        }


        mShapeLoadingDialog.setLoadingText("提交信息中...");
        mShapeLoadingDialog.setCanceledOnTouchOutside(false);
        mShapeLoadingDialog.show();


        Observable<JsonObject> observable;

        observable = Network
                .getAPIService()
                .toSelfOp("", tv_zq_id_str, edit_detail_str, tv_zq_phone_str,
                        creditorId, creditorName_str, tv_zq_email_str, tv_zq_other_phone_str,
                        mInfo.getBankruptcyProjectId(), "0", sendAddressid,
                        mUserType + "", getUserToken());


        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        mShapeLoadingDialog.dismiss();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }

                        if(mUserType== 1)
                        {
                            noticeDialog = new PublicNoticeDialog(ChangeToMeActivity.this, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (v.getId() == R.id.btn_save_notice) {
                                    }
                                    noticeDialog.dismiss();
                                    ChangeToMeActivity.super.finish();
                                }
                            }, 5, "提示", "变更为本人信息已经提交管理人审核，如有疑问，可于管理人联系。",
                                    "取消", "确认");
                            noticeDialog.show();

                        }else {
                            com.linkage.seapeak.utils.ToastUtil.getInstance(ChangeToMeActivity.this).showShortToast("设置完成！");
                            ChangeToMeActivity.super.finish();
                        }

                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mShapeLoadingDialog.dismiss();
                        super.onError(e);
                    }
                });

    }

}
